const state = {
	ctx: uni.createInnerAudioContext(),
	progress: '0%',
	duration: 0,
	status: 'stop',
	id: 0,
	list: [
		{_id:'1', name:'Concerning Hobbits', previewImg:'/static/play/item-bg-1.jpg', avatarUrl:'/static/avatar1.jpg', likeCount:'22, 033', liked: false, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'00:20:23'},
		{_id:'2', name:'如何做自我清理',previewImg:'/static/play/item-bg-2.jpg', avatarUrl:'/static/avatar2.jpg', likeCount:'22, 033', liked: true, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'00:12:23'},
		{_id:'3', name:'Firestone2 Concerning Hobbits',previewImg:'/static/play/item-bg-3.jpg', avatarUrl:'/static/avatar3.jpg', likeCount:'22, 033', liked: true, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'00:32:43'},
		{_id:'4', name:'Firestone3',previewImg:'/static/play/item-bg-1.jpg', avatarUrl:'/static/avatar1.jpg', likeCount:'22, 033', liked: true, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'00:05:09'},
		{_id:'5', name:'Firestone4',previewImg:'/static/play/item-bg-2.jpg', avatarUrl:'/static/avatar2.jpg', likeCount:'22, 033', liked: true, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'01:00:23'},
		{_id:'6', name:'Firestone5',previewImg:'/static/play/item-bg-3.jpg', avatarUrl:'/static/avatar3.jpg', likeCount:'22, 033', liked: true, author: 'author1', audio:'/static/temp/Concerning Hobbits.mp3', duration:'00:45:07'}
	],
	current: 0
}

const getters = {
	
}

const mutations = {
	init(state){
		state.status === 'play' && state.ctx.stop()
		state.progress = '0%'
		state.ctx.src = state.list[state.current].audio
		state.ctx.play()
		state.status = 'play'
		state.duration = state.ctx.duration
		state.ctx.onEnded(()=>{
			state.status = 'stop'
			state.id = 0
		})
	},
	progressMoving(state, payload){
		state.progress = Math.floor(state.ctx.currentTime / state.ctx.duration * 10000) / 100 + '%'
	},
	changeAudioStatus(state){
		if(state.status === 'play'){
			state.ctx.pause()
			state.status = 'pause'
		}else{
			state.ctx.play()
			state.status = 'play'
		}
	},
	setCurrent(state, payload){
		state.current = payload
		state.id = state.list[payload]._id
	},
	setId(state, payload){
		state.id = payload
		for(let i in state.list){
			if(state.list[i]._id == payload){
				state.current = i 
				break
			}
		}
	}
}

const actions = {
	
}

export default{
	state,
	getters,
	mutations,
	actions
}